package com.example.surveyapplication;

import bus.AccountBUS;
import dto.AccountDTO;
import help.Define;
import help.Utility;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SettingPasswordActivity extends Activity implements OnClickListener {
	Button btnSave;
	TextView lblSignOut;
	Button btnCancel;
	EditText txtNewPassword;
	EditText txtConfirmPassword;
	EditText txtOldPassowrd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_password);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		
		initComponents();
	}
	
	private void initComponents() {
		btnSave = (Button)findViewById(R.id.btnSave_SettingPassword);
		lblSignOut = (TextView)findViewById(R.id.lblSignOut_SettingPassword);
		btnCancel = (Button)findViewById(R.id.btnCancel_SettingPassword);
		txtNewPassword = (EditText)findViewById(R.id.txtNewPassword_SettingPassword);
		txtConfirmPassword = (EditText)findViewById(R.id.txtConfirmPassword_SettingPassword);
		txtOldPassowrd = (EditText)findViewById(R.id.txtOldPassword_SettingPassword);
		
		btnSave.setOnClickListener(this);
		lblSignOut.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.setting_password, menu);
		return true;
	}

	public void onClick(View view) {
		Intent intent;
		
		switch(view.getId()) {
		case R.id.btnSave_SettingPassword:
			String newPassword = txtNewPassword.getText().toString();
			String confirmPassword = txtConfirmPassword.getText().toString();
			String oldPassword = txtOldPassowrd.getText().toString();
			
			switch (Utility.changePassword(oldPassword, newPassword, confirmPassword)) {
			case Define.kEmpty:
				showAlert("Please check again.");
				break;
			case Define.kUnDifference:
				showAlert("Please enter another password.");
				break;
			case Define.kUnMatch:
				showAlert("Confirm password is wrong.");
				break;
			case Define.kFail:
				showAlert("Sorry, you can't change password.");
				break;
			case Define.kSuccess:
				showAlert("Change password is success.");
				super.onBackPressed();
				break;
			default:
				break;
			}
			break;
		case R.id.lblSignOut_SettingPassword:
			Utility.signOut();
			intent = new Intent(this, SignInActivity.class);
			startActivity(intent);
			break;
		case R.id.btnCancel_SettingPassword:
			super.onBackPressed();
			break;
		default:
			break;
		}
	}
	
	private void showAlert(String message) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("");
		alertDialog.setMessage(message);
		alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				}
				});
		alertDialog.show();
	}
}
